package com.gdssoft.pdaweb.service.impl;

import java.lang.Exception;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gdssoft.pdaweb.dao.PdaMdAssetInfoDao;
import com.gdssoft.pdaweb.model.PdaApplyRepair;
import com.gdssoft.core.util.DateUtil;
import com.gdssoft.pdaweb.model.CheckWorkOrder;
import com.gdssoft.pdaweb.model.RepairWorkOrder;
import com.gdssoft.pdaweb.service.EamService;
import com.gdssoft.pdaweb.service.rs.eam.Max2PDAService;
import com.gdssoft.pdaweb.service.rs.eam.Max2PDADelegate;

public class EamServiceImpl implements EamService {

	private transient final Log logger = LogFactory.getLog(getClass());
	
	private Max2PDADelegate max2PDADelegate;
	private PdaMdAssetInfoDao pdaMdAssetInfoDao;
	
	public EamServiceImpl(Max2PDAService max2PDAService){
		this.max2PDADelegate = max2PDAService.getMax2PDAPort();
	}
	
	public void setPdaMdAssetInfoDao(PdaMdAssetInfoDao pdaMdAssetInfoDao) {
		this.pdaMdAssetInfoDao = pdaMdAssetInfoDao;
	}
	
	/*
	public EamServiceImpl(
			RepairWorkOrderDao repairWorkOrderDao,
			CheckWorkOrderDao checkWorkOrderDao,
			PdaApplyRepairDao pdaApplyRepairDao
			) {
		this.repairWorkOrderDao=repairWorkOrderDao;
		this.checkWorkOrderDao=checkWorkOrderDao;
		this.pdaApplyRepairDao=pdaApplyRepairDao;
	}
	*/
	@Override
	public boolean syncRepairOrderStatus(RepairWorkOrder repairWorkOrder) {
		
		logger.info("同步参数：" + repairWorkOrder.toString());
		
		if (repairWorkOrder.getStatus().equals("2")) {
			return max2PDADelegate.returnWO(
				repairWorkOrder.getAppointPeople(), //用户编号
				repairWorkOrder.getAssignmentId().toString(),
				repairWorkOrder.getRemark()//工单的唯一标识
			);
		} else if (repairWorkOrder.getStatus().equals("9")) {
			boolean result = false;
			try{
				result = max2PDADelegate.changeStatusbyWONum(
					repairWorkOrder.getAppointPeople(), //用户编号
					repairWorkOrder.getWoNum(),
					repairWorkOrder.getSiteID(),
					6, //PDA状态9-已完成
					DateUtil.dateFormat(repairWorkOrder.getBeginTime()),
					DateUtil.dateFormat(repairWorkOrder.getEndTime())
				);
			}
			catch(Exception ex){
				logger.error("同步维修单失败，" ,ex);
			}
			return result;
		} else {
			logger.warn("状态不合法，拒绝处理！");
			return false;
		}
	}
	@Override
	public boolean syncCheckOrderStatus(CheckWorkOrder checkWorkOrder) {
		
		logger.info("同步参数：" + checkWorkOrder.toString());
		
		if (!checkWorkOrder.getStatus().equals("9")) {
			logger.warn("状态不合法，拒绝处理！");
			return false;
		}
		boolean result = false;
		try{
			result = max2PDADelegate.changeStatusbyWONum(
				checkWorkOrder.getAppointPeople(), //用户编号
				checkWorkOrder.getWoNum(),
				checkWorkOrder.getSiteID(),
				6, //PDA状态9-已完成
				DateUtil.dateFormat(checkWorkOrder.getBeginTime()),
				DateUtil.dateFormat(checkWorkOrder.getEndTime())
			);
		}
		catch(Exception ex){
			logger.error("同步巡检单失败，" ,ex);
		}
		return result;
	}
	
	@Override
	public String createApplyRepair(PdaApplyRepair pdaApplyRepair) {
		
		logger.info("同步参数：" + pdaApplyRepair.toString());
		
		int iWOType = -1;
		try {
			iWOType = Integer.parseInt(pdaApplyRepair.getWorkStatus());
		} catch(Exception ex) {
			logger.error(ex);
		}
		
		if (iWOType < 0) {
			logger.warn("WorkStatus 必须是数字");
			return "";
		}
		
		String assetNo = pdaMdAssetInfoDao.getByBarCode(pdaApplyRepair.getBarCode()).getAssetNo();
		if (null == assetNo){
			logger.warn("找不到barcode:"+pdaApplyRepair.getBarCode()+"对应的资产编号！");
		    return ""; 
		}
		
		if (iWOType==1 && (null == pdaApplyRepair.getReason() || "".equals(pdaApplyRepair.getReason()))) {
			logger.warn("非现场工单必须报修原因");
			return "";
		}
		

		int iEstdur = 0;
		try {
			iEstdur = Integer.parseInt(pdaApplyRepair.getEstdur());
		} catch(Exception ex) {
			logger.error(ex);
		}
		
		String description = pdaApplyRepair.getRemark();
		if(iWOType==1 && !pdaApplyRepair.getReason().equals("")){
			description=pdaApplyRepair.getReason()+","+description;
			
			logger.info("非现场工单，描述组合为：" + description);
		}
		
		String result = "";
		try{
			result = max2PDADelegate.createWO(
				pdaApplyRepair.getRecordeId(), 
				description, 
				assetNo, 
				null==pdaApplyRepair.getLocatedId()?"":pdaApplyRepair.getLocatedId(),
				iWOType, iEstdur);
		}
		catch(Exception ex){
			logger.error("创建维修单失败，" ,ex);
		}
		return result;
	}
	
	@Override
	public boolean createMR(String userid, String wonum, String itemnum,
			double itemqty, String location, String password) {
		logger.info("同步参数：" + userid + "," + wonum + "," + itemqty + "," + itemnum + "," + "," 
				+ location + "," + password);
		boolean result = false;
		if(userid == null || userid == "") return result;
		if(wonum == null || wonum == "") return result;
		if(itemnum == null || itemnum == "") return result;
		if(itemqty <=0) return result;
		try{
			result = max2PDADelegate.createMR(userid, wonum, itemnum, itemqty, location, password);
		}
		catch(Exception e){
			logger.info(e.getMessage());
		}
		return result;
	}
	
	
	
}
